package com.lanou3g.Code0517.study.Hw;


import com.lanou3g.Code0517.study.Hw.controller.Controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    private Controller controller = new Controller();

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String cpassword = request.getParameter("cpassword");
        String cname = request.getParameter("cname");
        String gender = request.getParameter("gender");
        String hobby = request.getParameter("hobby");


        Controller controller = new Controller();
        boolean registerSuccess = controller.register(username, password, cname, gender, hobby);
        String msg = "";
        if (cpassword.equals(password)) {
            if (registerSuccess) {
                msg="登录成功";

                request.getRequestDispatcher("/Hw/page.jsp").forward(request, response);
            } else {
                msg ="登录失败";
                request.getRequestDispatcher("/Hw/register.jsp").forward(request, response);
            }
        }
        request.getRequestDispatcher("/Hw/register.jsp").forward(request, response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);

    }
}
